package com.wang.java.basic.framework.queue.blockingqueue;

import com.wang.common.domain.User;

import java.util.concurrent.PriorityBlockingQueue;

/**
 * 优先级阻塞队列，PriorityQueue的阻塞版本
 * 数据结构：基于数组实现的小顶堆
 * 线程安全：是，基于ReentrantLock实现
 * 队列长度：无界，支持扩容
 * 阻塞性：阻塞
 */
public class PriorityBlockingQueueTest {
    public static void main(String[] args) throws InterruptedException {
        PriorityBlockingQueue<User> priorityBlockingQueue = new PriorityBlockingQueue(1, (a, b) -> {
            User user1 = (User) a;
            User user2 = (User) b;
            return user1.getId() - user2.getId();
        });
        priorityBlockingQueue.put(new User(3, "王五"));
        priorityBlockingQueue.put(new User(1, "张三"));
        priorityBlockingQueue.put(new User(2, "李四"));
        User user;
        while ((user = priorityBlockingQueue.take()) != null) {
            System.out.println(user);
        }
    }
}
